package org.example.cloud.controller;

import org.example.cloud.bean.EmailRequest;
import org.example.cloud.result.Result;
import org.example.cloud.service.implementation.EmailService;
import org.example.cloud.utils.RandomCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EmailController {
    @Autowired
    private EmailService emailService;

    //测试方法
    @PostMapping(value = "/api/getCode")
    public Result send(@RequestBody EmailRequest emailRequest){
//        //接收邮箱
//        String to = registerBean.getEmail();
        //主题
        String email = emailRequest.getEmail();
        String title = "验证码";
        //生成随机验证码
        String content = new RandomCode().getRandomCode();
        boolean result=false;
        //发送邮件
        try {
            result = emailService.send(email,title,content);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        if (result){
            return Result.error(200,content);
        }
        else {
            return Result.error(500,"发送失败");
        }
    }
}
